﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Linx.Update
{
    public class ConexaoDB
    {
        public string strConexao { get; set; }
        public SqlConnection cn = new SqlConnection();
       

        private string strServer;
        private string strUserDB;
        private string strPasswordDB;
        private string strDatabase;
        private string bWindowsAutentication;
        //public string strConnectionString;

        public void conectar(string connectionString)
        {

            try
            {
                string[] split = connectionString.Split(';');

                foreach (string s in split)
                {

                    if (s.Trim() != "")
                        //string[] valores = linha.Split(',');
                    strServer = split[0];
                    strDatabase = split[1];
                    strUserDB = split[2];
                    strPasswordDB = split[3];
                    bWindowsAutentication = split[4];
                }

                try
                {
                    if (bWindowsAutentication == "true")
                    {
                        strConexao = @"Data Source=" + strServer + ";Initial Catalog=" + strDatabase + ";Integrated Security=SSPI";
                        cn.ConnectionString = strConexao;
                    }
                    else
                    {
                        strConexao = @"Data Source=" + strServer + ";Initial Catalog=" + strDatabase + ";User ID=" + strUserDB + ";Password=" + strPasswordDB;
                        cn.ConnectionString = strConexao;
                    }

                    cn.Open();
                    GravaLog.WriteErrorLog("Conectou com sucesso !");

                }
                catch (Exception erro)
                {

                    GravaLog.WriteErrorLog("Erro ao tentar conectar: " + erro.Message);

                }


            }
            catch (Exception ex)
            {
                GravaLog.WriteErrorLog("Erro String de Conexão" + connectionString + ex.Message);
            }

        }

        public void Desconectar()
        {
            cn.Close();
            GravaLog.WriteErrorLog("Desconectou com sucesso !");
        }


    }
}
